<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:o="http://omnifaces.org/ui"
      xmlns:iqbs="http://xmlns.jcp.org/jsf/composite/iqbs">

    <h:body>
        <f:metadata>
            <f:viewParam name="id" value="#{managePermissionsPage.dvObject.id}"/>
            <f:viewAction action="#{dataverseSession.updateLocaleInViewRoot}"/>
            <f:viewAction action="#{managePermissionsPage.init}"/>
            <f:viewAction action="#{dataverseHeaderFragment.initBreadcrumbs(managePermissionsPage.dvObject, (managePermissionsPage.dvObject.instanceofDataverse ? bundle['dataverse.permissions.title'] : bundle['dataverse.permissions.dataset.title']))}"/>
        </f:metadata>
        <ui:composition template="/dataverse_template.xhtml">
            <ui:param name="pageTitle" value="#{bundle['dataverse.permissions.title']} - #{managePermissionsPage.dvObject.displayName}"/>
            <ui:param name="dataverse" value="#{managePermissionsPage.dvObject.dataverseContext}"/>
            <ui:param name="dataset" value="#{managePermissionsPage.dvObject.instanceofDataset ? managePermissionsPage.dvObject : null}"/>
            <ui:param name="version" value="#{managePermissionsPage.dvObject.instanceofDataset ? managePermissionsPage.dvObject.latestVersion : null}"/>
            <ui:param name="showMessagePanel" value="false"/>
            <ui:define name="body">
                <h:form id="rolesPermissionsForm" styleClass="margin-top">
                    <div class="panel-group">
                        <!-- Permissions Configure Panel -->
                        <p:fragment id="configureSettings">
                            <c:if test="#{managePermissionsPage.dvObject.instanceofDataverse}">
                                <div class="panel panel-default">
                                    <div data-toggle="collapse" data-target="#panelCollapsePermissions" class="panel-heading text-info">
                                        #{bundle['dataverse.permissions.title']} <span class="glyphicon glyphicon-chevron-up"/>
                                        
                                        <span class="text-muted small pull-right">#{bundle['dataverse.permissions.description']}</span>
                                    </div>
                                    <div id="panelCollapsePermissions" class="collapse in">
                                        <div class="panel-body">
                                            <p:fragment id="configureMessages">
                                                <div class="messagePanel">
                                                    <iqbs:messages  collapsible="true" rendered="#{managePermissionsPage.renderConfigureMessages}"/>
                                                </div>
                                            </p:fragment>
                                            <p:commandLink id="configureButton" styleClass="btn btn-default pull-right"
                                                           actionListener="#{managePermissionsPage.initAccessSettings}"
                                                           update="accessDialog"
                                                           oncomplete="PF('accessForm').show();">
                                                <span class="glyphicon glyphicon-cog"/> #{bundle['dataverse.permissions.access.accessBtn']}
                                            </p:commandLink>
                                            <p class="help-block clearfix">#{bundle['dataverse.permissions.tip']}</p>
                                            <div>
                                                <div class="form-group">
                                                    <label for="accessRadios">
                                                        #{bundle['dataverse.permissions.Q1']}
                                                    </label>
                                                    <div class="col-sm-offset-1">
                                                        <p class="form-control-static">
                                                            <h:outputText value="#{bundle['dataverse.permissions.Q1.answer1']}" rendered="#{empty managePermissionsPage.authenticatedUsersContributorRoleAlias}" />
                                                            <h:outputText value="#{bundle['dataverse.permissions.Q1.answer2']}" rendered="#{managePermissionsPage.authenticatedUsersContributorRoleAlias eq 'dvContributor'}" />
                                                            <h:outputText value="#{bundle['dataverse.permissions.Q1.answer3']}" rendered="#{managePermissionsPage.authenticatedUsersContributorRoleAlias eq 'dsContributor'}" />
                                                            <h:outputText value="#{bundle['dataverse.permissions.Q1.answer4']}" rendered="#{managePermissionsPage.authenticatedUsersContributorRoleAlias eq 'fullContributor'}" />
                                                        </p>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label for="contributorRadios">
                                                        #{bundle['dataverse.permissions.Q2']}
                                                    </label>
                                                    <div class="col-sm-offset-1">
                                                        <p class="form-control-static">
                                                            <ui:fragment rendered="#{managePermissionsPage.defaultContributorRoleAlias eq 'contributor'}">
                                                                #{bundle.editor} <span class="text-muted">#{bundle['dataverse.permissions.Q2.answer.editor.description']}</span>
                                                            </ui:fragment>
                                                            <ui:fragment rendered="#{managePermissionsPage.defaultContributorRoleAlias eq 'curator'}">
                                                                #{bundle.curator} <span class="text-muted">#{bundle['dataverse.permissions.Q2.answer.curator.description']}</span>
                                                            </ui:fragment>
                                                            <ui:fragment rendered="#{managePermissionsPage.isCustomDefaultContributorRole()}">
                                                                #{managePermissionsPage.customDefaultContributorRoleName} <span class="text-muted">#{managePermissionsPage.customDefaultContributorRoleDescription}</span>
                                                            </ui:fragment>
                                                        </p>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </c:if>
                        </p:fragment>

                        <!-- Role Assignments Panel -->
                        <div class="panel panel-default">
                            <div data-toggle="collapse" data-target="#panelCollapseUsersGroups" class="panel-heading text-info">
                                #{bundle['dataverse.permissions.usersOrGroups']} <span class="glyphicon #{!managePermissionsPage.dvObject.instanceofDataverse ? 'glyphicon-chevron-up' : 'glyphicon-chevron-down'}"/>
                                
                                <span class="text-muted small pull-right">#{bundle['dataverse.permissions.usersOrGroups.description']}</span>
                            </div>
                            <div id="panelCollapseUsersGroups" class="collapse #{!managePermissionsPage.dvObject.instanceofDataverse ? 'in' : ''}">
                                <div class="panel-body">
                                    <p:fragment id="assignmentMessages">
                                        <div class="messagePanel">
                                            <iqbs:messages collapsible="true" rendered="#{managePermissionsPage.renderAssignmentMessages}"/>
                                        </div>
                                    </p:fragment>
                                    <div class="margin-bottom text-right">
                                        <p:commandLink id="userGroupsAdd" styleClass="btn btn-default"
                                                       actionListener="#{managePermissionsPage.initAssigneeDialog}"
                                                       update="userGroupDialog roleAssignmentHistory"
                                                       oncomplete="PF('userGroupsForm').show();handleResizeDialog('userGroupDialog');">
                                            <span class="glyphicon glyphicon-user"/> #{bundle['dataverse.permissions.usersOrGroups.assignBtn']}
                                        </p:commandLink>
                                    </div>
                                    <div>
                                        <p>
                                            <h:outputFormat styleClass="highlightBold" value="#{managePermissionsPage.roleAssignments.size()} #{bundle['dataverse.permissionsFiles.files.roleAssignees.label']}">
                                                <f:param value="#{managePermissionsPage.roleAssignments.size()}"/>
                                            </h:outputFormat>
                                        </p>
                                        
                                        <p:dataTable id="assignedRoles" var="roleAssignment" value="#{managePermissionsPage.roleAssignments}" sortBy="#{roleAssignment.assigneeDisplayInfo.title}">
                                            <p:column width="35%" headerText="#{bundle['dataverse.permissions.usersOrGroups.tabHeader.userOrGroup']}" sortBy="#{roleAssignment.assigneeDisplayInfo.title}">
                                                <h:outputText value="#{roleAssignment.assigneeDisplayInfo.title}"/>
                                                <h:outputText value=" (#{roleAssignment.assigneeDisplayInfo.affiliation})" rendered="#{!empty roleAssignment.assigneeDisplayInfo.affiliation}"/>
                                            </p:column>
                                            <p:column width="15%" headerText="#{bundle['dataverse.permissions.usersOrGroups.tabHeader.id']}" sortBy="#{roleAssignment.roleAssignment.assigneeIdentifier}">
                                                <h:outputText value="#{roleAssignment.roleAssignment.assigneeIdentifier}"/>
                                            </p:column>
                                            <p:column width="20%" class="text-center" headerText="#{bundle['dataverse.permissions.usersOrGroups.tabHeader.role']}" sortBy="#{roleAssignment.roleName}">
                                                <h:outputText value="#{roleAssignment.roleName}"/>
                                            </p:column>
                                            <p:column width="30%" class="text-center" headerText="#{bundle['dataverse.permissions.usersOrGroups.tabHeader.action']}">
                                                <h:outputFormat styleClass="text-muted italic" value="#{bundle['dataverse.permissions.usersOrGroups.assignedAt']}" rendered="#{managePermissionsPage.dvObject ne roleAssignment.definitionPoint}">
                                                    <f:param value="#{roleAssignment.assignedDvName}"/>
                                                </h:outputFormat>
                                                <p:commandLink styleClass="btn btn-default"
                                                               rendered="#{managePermissionsPage.dvObject eq roleAssignment.definitionPoint}"
                                                               onclick="PF('confirmation').show()">
                                                    <f:setPropertyActionListener value="#{roleAssignment.roleAssignment}" target="#{managePermissionsPage.selectedRoleAssignment}" />
                                                    <span class="glyphicon glyphicon-remove"/> #{bundle['dataverse.permissions.usersOrGroups.removeBtn']}
                                                </p:commandLink>
                                            </p:column>
                                        </p:dataTable>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Roles Panel -->
                        <div class="panel panel-default">
                            <div data-toggle="collapse" data-target="#panelCollapseRoles" class="panel-heading text-info">
                                #{bundle['dataverse.permissions.roles']} <span class="glyphicon glyphicon-chevron-down"/>
                                
                                <span class="text-muted small pull-right">#{bundle['dataverse.permissions.roles.description']}</span>
                            </div>
                            <div id="panelCollapseRoles" class="collapse">
                                <div class="panel-body">
                                    <p:fragment id="roleMessages">
                                        <div class="messagePanel">
                                            <iqbs:messages  collapsible="true" rendered="#{managePermissionsPage.renderRoleMessages}"/>
                                        </div>
                                    </p:fragment>
                                    <div class="margin-bottom text-right">
                                        <p:commandLink id="rolesAdd" styleClass="btn btn-default"
                                                       rendered="#{managePermissionsPage.dvObject.instanceofDataverse and permissionsWrapper.canManagePermissions(managePermissionsPage.dvObject)
                                                                    and dataverseSession.user.superuser}"
                                                       actionListener="#{managePermissionsPage.createNewRole}"
                                                       oncomplete="PF('roleForm').show();handleResizeDialog('rolesPermissionsDialog');"
                                                       update=":#{p:resolveClientId('rolesPermissionsForm:editRolePanel', view)}">
                                            <span class="glyphicon glyphicon-plus"/> #{bundle['dataverse.permissions.roles.add']}
                                        </p:commandLink>
                                    </div>
                                    <p:fragment id="roleDisplay">
                                        <ui:repeat id="roleList" value="#{managePermissionsPage.availableRoles}" var="role">
                                            <f:passThroughAttribute name="class" value="list-unstyled" />
                                            <div class="form-group col-sm-12 #{!empty role.owner ? 'bg-info' : 'bg-warning'}">
                                                <span class="highlightBold">#{role.name}</span>
                                                <h:outputText value=" - #{role.description}" rendered="#{!empty role.description}"/>
                                                <p:commandLink title="#{bundle['dataverse.permissions.roles.edit']}" styleClass="icon-inline-action bootstrap-button-tooltip"
                                                               rendered="#{!empty role.owner and managePermissionsPage.dvObject.instanceofDataverse and permissionsWrapper.canManagePermissions(role.owner)
                                                                            and dataverseSession.user.superuser}"
                                                               actionListener="#{managePermissionsPage.editRole(role.id)}"
                                                               oncomplete="PF('roleForm').show();handleResizeDialog('rolesPermissionsDialog');"
                                                               update=":#{p:resolveClientId('rolesPermissionsForm:editRolePanel', view)}"><span class="glyphicon glyphicon-pencil"></span></p:commandLink>
                                                <p:commandLink title="#{bundle['dataverse.permissions.roles.copy']}" styleClass="icon-inline-action bootstrap-button-tooltip"
                                                               rendered="#{managePermissionsPage.dvObject.instanceofDataverse and permissionsWrapper.canManagePermissions(managePermissionsPage.dvObject)
                                                                            and dataverseSession.user.superuser}"
                                                               actionListener="#{managePermissionsPage.cloneRole(role.id)}"
                                                               oncomplete="PF('roleForm').show();handleResizeDialog('rolesPermissionsDialog');"
                                                               update=":#{p:resolveClientId('rolesPermissionsForm:editRolePanel', view)}"><span class="glyphicon glyphicon-duplicate"></span></p:commandLink>
                                                <div>
                                                    <ui:repeat value="#{role.permissions().toArray()}" var="prm">
                                                        <span class="label label-default">#{bundle['permission.'.concat(prm).concat('.label')]}</span>
                                                    </ui:repeat>
                                                </div>
                                            </div>
                                        </ui:repeat>
                                    </p:fragment>
                                </div>
                            </div>
                        </div>
                        <!-- Role Assignment History Panel -->
                        <o:importConstants type="edu.harvard.iq.dataverse.settings.FeatureFlags" />
                        <div class="panel panel-default" rendered="#{FeatureFlags.ROLE_ASSIGNMENT_HISTORY.enabled()}">
                          <div data-toggle="collapse" data-target="#panelCollapseHistory" class="panel-heading text-info">
                            #{bundle['dataverse.permissions.history']} <span class="glyphicon glyphicon-chevron-down" /> <span class="text-muted small pull-right">#{bundle['dataverse.permissions.history.description']}</span>
                          </div>
                          <div id="panelCollapseHistory" class="collapse">
                            <p:panel id="historyPanel" deferred="true" deferredMode="visible">
                                <div class="panel-body">
                                    <div class="button-group margin-bottom text-right">
                                        <h:outputLink value="#{managePermissionsPage.signedUrlForRAHistoryCsv}" 
                                                     styleClass="btn btn-default" 
                                                     onclick="downloadRAHistoryCSV(this.href, '#{managePermissionsPage.permissionsHistoryFilename}'); return false;">
                                            <span class="glyphicon glyphicon-download"></span> #{bundle['dataverse.permissions.history.download']}
                                        </h:outputLink>
                                    </div>
                                    <p:dataTable id="roleAssignmentHistory" widgetVar="roleAssignmentHistory" var="historyEntry" value="#{managePermissionsPage.roleAssignmentHistory}"
                                        sortMode="multiple" paginator="true" rows="10"
                                        paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                        rowsPerPageTemplate="5,10,15">
                                        <p:column headerText="#{bundle['dataverse.permissions.history.assignee']}" sortBy="#{historyEntry.assigneeIdentifier}">
                                            <h:outputText value="#{historyEntry.assigneeIdentifier}" />
                                        </p:column>
                                        <p:column headerText="#{bundle['dataverse.permissions.history.role']}" sortBy="#{historyEntry.roleName}">
                                            <h:outputText value="#{historyEntry.roleName}" />
                                        </p:column>
                                        <p:column headerText="#{bundle['dataverse.permissions.history.assignedBy']}" sortBy="#{historyEntry.assignedBy}">
                                            <h:outputText value="#{historyEntry.assignedBy}" />
                                        </p:column>
                                        <p:column headerText="#{bundle['dataverse.permissions.history.assignedAt']}" sortBy="#{historyEntry.assignedAt}">
                                            <h:outputText value="#{historyEntry.assignedAt}">
                                                <f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss" />
                                            </h:outputText>
                                        </p:column>
                                        <p:column headerText="#{bundle['dataverse.permissions.history.revokedBy']}" sortBy="#{historyEntry.revokedBy}">
                                            <h:outputText value="#{historyEntry.revokedBy}" />
                                        </p:column>
                                        <p:column headerText="#{bundle['dataverse.permissions.history.revokedAt']}" sortBy="#{historyEntry.revokedAt}">
                                            <h:outputText value="#{historyEntry.revokedAt}">
                                                <f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss" />
                                            </h:outputText>
                                        </p:column>
                                    </p:dataTable>
                                </div>
                            </p:panel>
                        </div>
                      </div>
                    </div>

                    <!-- Users/Groups Popup -->
                    <!-- Defined this way as we may reuse the include not in a popup -->
                    <p:dialog id="accessDialog" styleClass="largePopUp" header="#{bundle['dataverse.permissions.accessDialog.header']}" widgetVar="accessForm" modal="true">
                        <p:focus context="accessDialog"/>
                        <ui:include src="permissions-configure.xhtml"/>
                        <div class="button-block">
                            <p:commandButton value="#{bundle['saveChanges']}" styleClass="btn btn-default"
                                           update=":#{p:resolveClientId('rolesPermissionsForm:configureSettings', view)} @([id$=assignedRoles]) assignedRoles roleAssignmentHistory @([id$=Messages])"
                                           actionListener="#{managePermissionsPage.saveConfiguration}"
                                           oncomplete="PF('accessForm').hide()"/>
                            <button class="btn btn-link" onclick="PF('accessForm').hide()" type="button">
                                #{bundle.cancel}
                            </button>
                        </div>
                    </p:dialog>

                    <!-- Users/Groups Popup -->
                    <ui:include src="roles-assign.xhtml"/>
                    <!-- Roles Edit Popup -->
                    <ui:include src="roles-edit.xhtml"/>
                    
                    <p:dialog id="accessRemoveConfirm" header="#{bundle['dataverse.permissions.usersOrGroups.removeBtn']}" widgetVar="confirmation" modal="true">
                        <p class="help-block">
                            <span class="glyphicon glyphicon-warning-sign text-danger"/> <span class="text-danger">#{bundle['dataverse.permissions.usersOrGroups.removeBtn.confirmation']}</span>
                        </p>
                        <div class="button-block">
                            <p:commandButton value="#{bundle.continue}" styleClass="btn btn-default" onclick="PF('confirmation').hide()" 
                                             action="#{managePermissionsPage.removeRoleAssignment()}" 
                                             update=":#{p:resolveClientId('rolesPermissionsForm:configureSettings', view)} assignedRoles roleAssignmentHistory @([id$=Messages])" />
                            <button class="btn btn-link" onclick="PF('confirmation').hide()" type="button">
                                #{bundle.cancel}
                            </button>
                        </div>
                    </p:dialog>
                </h:form>
                <script src="#{resource['js/rahistory_utils.js']}?version=#{settingsWrapper.appVersion}"></script>
            </ui:define>
        </ui:composition>
    </h:body>
</html>
